package com.example.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Carousel)表服务实现类
 *
 * @author makejava
 * @since 2024-02-29 21:02:33
 */
@Service("carouselService")
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 新增轮播图
     *
     * @param carousel
     */
    @Override
    public void addCarousel(Carousel carousel) {
        try {
            carouselMapper
                    .addCarousel(carousel);
        } catch (Exception e) {
            throw new ServiceException("新增轮播图失败");
        }
    }


    /**
     * 条件查询
     *
     * @param title
     * @param detail
     * @param startTime
     * @param endTime
     * @param status
     * @return
     */
    @Override
    public List<Carousel> selectByManyCondition(String title, String detail, String startTime, String endTime, Integer status) {
        try {
            // 处理时间段
            if (StrUtil.isBlank(startTime)) {
                startTime = "1000-01-01 00:00:00";
            }
            if (StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }
            if (StrUtil.isBlank(status.toString())) {
                throw new ServiceException("请选择状态");
            }
            return carouselMapper.selectByManyCondition(title, detail, startTime, endTime, status);
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }


    /**
     * 条件查询（不包含状态）
     *
     * @param title
     * @param detail
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Carousel> selectByManyConditionNoStatus(String title, String detail, String startTime, String endTime) {
        try {
            // 处理时间段
            if (StrUtil.isBlank(startTime)) {
                startTime = "1000-01-01 00:00:00";
            }
            if (StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }
            return carouselMapper.selectByManyConditionNoStatus(title, detail, startTime, endTime);
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }


    /**
     * 条件查询     分页
     *
     * @param currentPage
     * @param pageSize
     * @param title
     * @param detail
     * @param startTime
     * @param endTime
     * @param status
     * @return
     */
    @Override
    public Page<Carousel> selectByManyConditionInPage(Integer currentPage, Integer pageSize, String title, String detail, String startTime, String endTime, Integer status) {
        try {
            // 计算起始行
            Integer startRow = (currentPage - 1) * pageSize;

            // 处理时间段
            if (StrUtil.isBlank(startTime)) {
                startTime = "1000-01-01 00:00:00";
            }
            if (StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }

            if (StrUtil.isBlank(status.toString())) {
                throw new ServiceException("请选择状态");
            }

            // 查询总记录数
            int totalCount = carouselMapper.selectByManyCondition(title, detail, startTime, endTime, status).size();
            List<Carousel> carousels = carouselMapper.selectByManyConditionInPage(startRow, pageSize, title, detail, startTime, endTime, status);

            // 封装返回结果
            Page<Carousel> page = new Page<>();
            page.setList(carousels);
            page.setTotal(totalCount);
            return page;
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }


    /**
     * 多条件查询 无状态参数 分页
     *
     * @param currentPage
     * @param pageSize
     * @param title
     * @param detail
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Page<Carousel> selectByManyConditionNoStatusInPage(Integer currentPage, Integer pageSize, String title, String detail, String startTime, String endTime) {
        try {
            // 计算起始行
            Integer startRow = (currentPage - 1) * pageSize;

            // 处理时间段
            if (StrUtil.isBlank(startTime)) {
                startTime = "1000-01-01 00:00:00";
            }
            if (StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }

            // 查询总记录数
            int totalCount = carouselMapper.selectByManyConditionNoStatus(title, detail, startTime, endTime).size();
            List<Carousel> carousels = carouselMapper.selectByManyConditionNoStatusInPage(startRow, pageSize, title, detail, startTime, endTime);

            // 封装返回结果
            Page<Carousel> page = new Page<>();
            page.setList(carousels);
            page.setTotal(totalCount);
            return page;
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }


    /**
     * 获取所有的轮播图信息
     *
     * @return
     */
    @Override
    public List<Carousel> selectAll() {
        try {
            return carouselMapper.selectAll();
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }


    /**
     * 获取最新的五条轮播图信息
     *
     * @return
     */
    @Override
    public List<Carousel> selectLatest() {
        try {
            return carouselMapper.selectLatest();
        } catch (Exception e) {
            throw new ServiceException("获取轮播图失败");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            throw new ServiceException("请选中后点击删除选中");
        }
        try {
            for (Integer id : ids) {
                carouselMapper.deleteById(id);
            }
        } catch (Exception e) {
            throw new ServiceException("删除失败");
        }
    }


    /**
     * 分页查找
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<Carousel> selectByPage(Integer currentPage, Integer pageSize) {
        // 计算起始行
        Integer startRow = (currentPage - 1) * pageSize;

        // 查询总记录数
        int totalCount = carouselMapper.selectAll().size();

        // 查询分页数据
        List<Carousel> Carousels = carouselMapper.selectByPage(startRow, pageSize);

        // 封装返回结果
        Page<Carousel> page = new Page<>();
        page.setList(Carousels);
        page.setTotal(totalCount);
        return page;
    }


    /**
     * 根据ID删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        try {
            carouselMapper.deleteById(id);
        } catch (Exception e) {
            throw new ServiceException("删除失败");
        }
    }


    /**
     * 根据ID更新信息
     *
     * @param carousel
     */
    @Override
    public void updateById(Carousel carousel) {
        try {
            carouselMapper.updateById(carousel);
        } catch (Exception e) {
            throw new ServiceException("更新失败");
        }
    }

    /**
     * 根据ID更新状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        if (StrUtil.isBlank(id.toString())) {
            throw new ServiceException("id不能为空");
        }
        if (StrUtil.isBlank(status.toString())) {
            throw new ServiceException("状态不能为空");
        }
        try {
            carouselMapper.updateStatus(id, status);
        } catch (Exception e) {
            throw new ServiceException("更新状态失败");
        }
    }

}
